<?php
/**
 * @author : xjw129xjt 531201650@qq.com
 */

namespace app\core\model;

use think\Model;

class Picture extends Model
{
    protected $table = 'picture';

    public function addPicture($data = array())
    {
        $this->driver = $data['driver'];
        $this->path = $data['path'];
        $this->md5 = $data['md5'];
        $this->sha1 = $data['sha1'];
        $this->status = isset($data['status']) ? $data['status'] : 1;
        $this->create_time = isset($data['create_time']) ? $data['create_time'] : time();
        $this->width = $data['width'];
        $this->height = $data['height'];
        $this->ext = $data['ext'];
        $this->size = $data['size'];
        $this->extra = $data['extra'];
        $this->save();
        $id = $this->id;
        return $id;
    }


    public function getPicture($id = 0, $field = null)
    {
        //todo 缓存
        $obj = $this->get($id);
        $picture = $obj ? $obj->toArray() : false;
        if ($picture) {
            list($p0, $p1) = $this->renderImgPath($picture['path']);
            $picture['rpath'] = $p0;
            $picture['path'] = $p1;
            $picture['extra'] = json_decode($picture['extra'], true);
        }
        return $field ? $picture[$field] : $picture;
    }

    public function renderImgPath($path)
    {
        $root = get_root();
        $p0 = './' . trim($path, '/');
        $p1 = $root . '/' . trim($path, '/');
        return [$p0, $p1];
    }

    public function checkPictureExist($md5)
    {
        $picture = $this->getByMd5($md5);
        return $picture ? $picture['id'] : false;
    }


    public function thumb($id, $width = 300, $height = 300)
    {
        $picture = $this->getPicture($id);
        if (!$picture) {
            return '';
        }
        return $this->thumbByPath($picture['path'], $width, $height);
    }


    public function thumbByPath($path, $width = 300, $height = 300)
    {
        list($p0, $p1) = $this->renderImgPath($path);
        $thumb_rpath = $this->getThumbPath($p0, $width, $height);
        if (!file_exists($thumb_rpath)) {
            $image = \think\Image::open($p0);
            $image->thumb($width, $height, \think\Image::THUMB_CENTER)->save($thumb_rpath);
        }
        return $this->getThumbPath($p1, $width, $height);
    }

    public function getThumbPath($path, $width = 300, $height = 300)
    {
        $array = explode('.', $path);
        $end = end($array);
        array_pop($array);
        return implode('.', $array) . '_' . $width . '_' . $height . '.' . $end;
    }

    public function info()
    {

    }


    public function getSimplePicture($id)
    {
        $picture = $this->getPicture($id);
        $return['id'] = $id;
        $return['path'] = $picture['path'];
        $thumb_size = min($picture['width'], $picture['height'], 300);
        if ($picture['driver'] == 'local') {
            $new_img = $this->thumb($id, $thumb_size, $thumb_size);
        } else {
            //todo 其他方式的实现
        }
        $return['thumb'] = $new_img;
        $return['size'] = $picture['width'] . 'x' . $picture['height'];
        return $return;

    }


    public function crop($id, $crop)
    {
        $picture = $this->getPicture($id);
        list($p0, $p1) = $this->renderImgPath($picture['path']);

        $rpath = $this->_getCropPath($p0, $crop);
        if (!file_exists($rpath)) {
            $image = \think\Image::open($p0);
            $image->crop($crop['w'], $crop['h'], $crop['x'], $crop['y'])->save($rpath);
        }
        return $this->_getCropPath($p1, $crop);
    }

    private function _getCropPath($path, $crop)
    {
        $array = explode('.', $path);
        $end = end($array);
        array_pop($array);
        return implode('.', $array) . '_x' . $crop['x'] . 'y' . $crop['y'] . '_' . $crop['w'] . '_' . $crop['h'] . '.' . $end;
    }


    public function renderPicturePath($path)
    {
        return $this->getThumbPath($path);
    }


    public function getPicExt($ext = '')
    {
        $arr = [
            1 => 'GIF',
            2 => 'JPG',
            3 => 'PNG',
            4 => 'SWF',
            5 => 'PSD',
            6 => 'BMP',
            7 => 'TIFF(intel byte order)',
            8 => 'TIFF(motorola byte order)',
            9 => 'JPC',
            10 => 'JP2',
            11 => 'JPX',
            12 => 'JB2',
            13 => 'SWC',
            14 => 'IFF',
            15 => 'WBMP',
            16 => 'XBM'
        ];
        return $ext ? $arr[$ext] : $arr;
    }
}

